package cn.tuning.controller;


import cn.tuning.entity.OrderInfo;
import cn.tuning.entity.RiderInfo;
import cn.tuning.pojo.APIResult;
import cn.tuning.service.OrderService;
import cn.tuning.utils.IdWorkerUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.Objects;

/**
 * @author lixiaolong
 * @date 2021-08-06 22:01
 * @description:
 */

@RestController
@RequestMapping("/order")
@ResponseBody
public class OrderController {

    @Autowired
    OrderService orderService;
    /**
     * 用户发布订单
     * @param orderInfo  订单对象
     * @return
     */
    @PostMapping("/publish")
    public APIResult publish(OrderInfo orderInfo, HttpServletRequest request){
        APIResult apiResult = null;
        if (Objects.isNull(orderInfo) ||//判断是否为空
                Objects.isNull(orderInfo.getCustomerId()) ||//顾客ID
                orderInfo.getCommission()<=0 || //佣金
                Objects.isNull(orderInfo.getReceivedAddress()) || //接收地址
//                Objects.isNull(orderInfo.getDeliveryTime()) || //提交时间
                orderInfo.getWeight()<=0 ||  //  重量
                Objects.isNull(orderInfo.getPickUpCode()))//取件码
        {
            apiResult = APIResult.parameterMiss(); //400 : 参数缺失
            return apiResult;
        }
        if(Objects.isNull(request.getSession().getAttribute(orderInfo.getCustomerId()))){
            apiResult = APIResult.notLogin();//尚未请先登录
            return apiResult;
        }
        Date createTime = new Date();//时间
        IdWorkerUtil worker = new IdWorkerUtil(1,1,1);
        String id = String.valueOf(worker.nextId());//生成订单id
        orderInfo.setCreateTime(createTime);
        orderInfo.setId(id);
        apiResult = orderService.publishOrder(orderInfo);//修改信息
        return apiResult;
    }

    /**
     * 骑手接单
     * @param riderId 骑手id
     * @param orderId 订单id
     * @return
     */
    @PostMapping("/takeOrder")
    public APIResult takeOrder(@RequestParam(value = "riderId", required = false) String riderId,
                               @RequestParam(value = "orderId" , required = false) String orderId,
                               HttpServletRequest request){
        //判断登录
        APIResult apiResult = null;
        if(Objects.isNull(request.getSession().getAttribute(riderId))){
            apiResult = APIResult.notLogin();//尚未请先登录
            return apiResult;
        }
        if (Objects.isNull(riderId) || Objects.isNull(orderId)){
            apiResult = APIResult.parameterMiss(); //400 : 参数缺失
            return apiResult;
        }
        apiResult = orderService.takeOrder(riderId,orderId);
       return apiResult;
    }

    /**
     * 骑手完成订单
     * @param riderId 骑手id
     * @param orderId 订单id
     * @param request
     * @return
     */
    @PostMapping("/riderFinish")
    public APIResult riderFinish(@RequestParam(value = "riderId", required = false) String riderId,
                                 @RequestParam(value = "orderId" , required = false) String orderId,
                                 HttpServletRequest request){
        //判断登录
        APIResult apiResult = null;
        if(Objects.isNull(request.getSession().getAttribute(riderId))){
            apiResult = APIResult.notLogin();//尚未请先登录
            return apiResult;
        }
        if (Objects.isNull(riderId) || Objects.isNull(orderId)){
            apiResult = APIResult.parameterMiss(); //400 : 参数缺失
            return apiResult;
        }
        apiResult = orderService.finishOrder(riderId,orderId);
        return apiResult; //
    }

    /**
     * 用户查询自己所有订单
     * @param customerId 客户id
     * @return
     */
    @PostMapping("/customerQueryOrder")
    public APIResult customerQueryOrder(@RequestParam(value = "customerId" ,required = false)
                                                      String customerId, HttpServletRequest request){
        APIResult apiResult = null;
        if(Objects.isNull(request.getSession().getAttribute(customerId))){ //判断是否登录
            apiResult = APIResult.notLogin();//尚未登录 请先登录
           return apiResult;
        }
        if (Objects.isNull(customerId)){//判断是否为空
            apiResult = APIResult.errorOperate(); //405 : 操作失败,请求操作的资源不存在
            return  apiResult;
        }
        apiResult = orderService.customerQueryOrder(customerId);
        return apiResult; //200 SUCCESS 数据
    }

    /**
     * 骑手查询自己所有订单
     * @param riderId 骑手id
     * @param request
     * @return
     */
    @PostMapping("/riderQueryOrder")
    public APIResult riderQueryOrder(@RequestParam(value = "riderId", required = false) String riderId,
                                     HttpServletRequest request){
        APIResult apiResult = null;
        if(Objects.isNull(request.getSession().getAttribute(riderId))){ //判断是否登录
            apiResult = APIResult.notLogin();//尚未登录 请先登录
            return apiResult;
        }
        if (Objects.isNull(riderId)){//判断是否为空
            apiResult = APIResult.errorOperate(); //405 : 操作失败,请求操作的资源不存在
            return apiResult;
        }
        apiResult = orderService.riderQueryOrder(riderId);
        return apiResult; //200 SUCCESS 数据
    }

    /**
     * 接单广场:所有已发布未被接收的订单
     */
    @GetMapping("/receivePlaza")
    public APIResult  receivePlaza(@RequestParam(value = "riderId", required = false) String riderId,
                                   HttpServletRequest request){
        APIResult apiResult = null;
        if(Objects.isNull(request.getSession().getAttribute(riderId))){ //判断是否登录
            apiResult = APIResult.notLogin();//尚未登录 请先登录
            return apiResult;
        }
        apiResult = orderService.riderReceive(riderId);
        return apiResult;
    }



    /**
     * 骑手完善信息
     * @return riderInfo
     */
    @PostMapping("/riderRegister")
    public APIResult riderRegister(RiderInfo riderInfo, HttpServletRequest request){
        APIResult apiResult = null;
        if(Objects.isNull(request.getSession().getAttribute(riderInfo.getId()))){ //判断是否登录
            apiResult = APIResult.notLogin();//尚未登录 请先登录
            return apiResult;
        }
        apiResult = orderService.riderRegister(riderInfo);
        return apiResult;
    }


    /**
     * 我的资料
     */
    @PostMapping("/homePage")
    public APIResult  homePage(@RequestParam(value = "username", required = false) String username,
                                   HttpServletRequest request){
        APIResult apiResult = null;
        if(Objects.isNull(request.getSession().getAttribute(username))){ //判断是否登录
            apiResult = APIResult.notLogin();//尚未登录 请先登录
            return apiResult;
        }
        apiResult = orderService.getUserMassage(username);
        return apiResult;
    }

    /**
     * 骑手资料
     */
    @PostMapping("/riderHome")
    public APIResult  riderHome(@RequestParam(value = "riderId", required = false) String riderId,
                               HttpServletRequest request){
        APIResult apiResult = null;
        if(Objects.isNull(request.getSession().getAttribute(riderId))){ //判断是否登录
            apiResult = APIResult.notLogin();//尚未登录 请先登录
            return apiResult;
        }
        apiResult = orderService.getRiderMassage(riderId);
        return apiResult;
    }



}
